package frist50;

import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

public class T0003 {

    public int lengthOfLongestSubstring(String s) {
        Map<Character,Integer>  hashmap=new HashMap<>();
        int left=0,sum=1,tmp=1;
        if (s.length()==0|s.length()==1)return s.length();
        hashmap.put(s.charAt(0),0);
        for (int i = 1; i <s.length() ; i++) {
            char c=s.charAt(i);
            if(!hashmap.containsKey(c)){
                hashmap.put(c,i);
                tmp++;
            }else {
                int right=hashmap.get(c);
                while (left<=right){
                    hashmap.remove(s.charAt(left));
                    left++;
                    tmp--;
                }
                hashmap.put(c,i);
                tmp++;
            }
            sum=Math.max(sum,tmp);
        }
        return sum;

    }
    @Test
    public void test() {
        System.out.println(lengthOfLongestSubstring("aab"));

    }
}
